package com.gzyj.flink.gps.addtionalfnfo;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONScanner;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.gzyj.flink.gps.addtionalfnfo.subiao.*;
import com.gzyj.flink.gps.addtionalfnfo.yuebiao.JTT808_0x0200_0xF1;
import com.gzyj.flink.gps.addtionalfnfo.yuebiao.JTT808_0x0200_0xF2;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;

/**
 * @author lhj
 */
public class AbstractResultDeserializer implements ObjectDeserializer {

    public static final String CHARACTERISTIC = "\"ID\":";

    private String version;

    public AbstractResultDeserializer(String version) {
        this.version = version;
    }


    @Override
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object object) {
        String text = (String) parser.input; //需要反序列化的文本
        int begin = ((JSONScanner) parser.lexer).pos() + 1;//当前反序列化进行到的位置
        text = text.substring(begin, findEndPoint(text, begin));

        if (text.toUpperCase().contains(CHARACTERISTIC + "1,")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x01.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + "2,")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x02.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x25 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x25.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x6A + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x6A.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x30 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x30.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x31 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x31.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x03 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x03.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x04 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x04.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x05 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x05.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x06 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x06.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x11 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x11.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x12 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x12.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x13 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x13.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x14 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x14.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x15 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x15.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x16 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x16.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x17 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x17.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x18 + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x18.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x2A + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x2A.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x2B + ",")) {
            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x2B.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;
        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x64 + ",")) {

            if (StringUtils.isNoneEmpty(this.version) && this.version.equalsIgnoreCase("2013")) {
                JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x64.class)
                        .deserialze(parser, type, object);
                return (T) addtionalInfo;
            } else {
                JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(com.gzyj.flink.gps.addtionalfnfo.yuebiao.JTT808_0x0200_0x64.class)
                        .deserialze(parser, type, object);
                return (T) addtionalInfo;
            }

        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x65 + ",")) {

            if (StringUtils.isNoneEmpty(this.version) && this.version.equalsIgnoreCase("2013")) {
                JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x65.class)
                        .deserialze(parser, type, object);
                return (T) addtionalInfo;
            } else {
                JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(com.gzyj.flink.gps.addtionalfnfo.yuebiao.JTT808_0x0200_0x65.class)
                        .deserialze(parser, type, object);
                return (T) addtionalInfo;
            }

        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x66 + ",")) {

            if (StringUtils.isNoneEmpty(this.version) && this.version.equalsIgnoreCase("2013")) {
                JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x66.class)
                        .deserialze(parser, type, object);
                return (T) addtionalInfo;
            } else {
                JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(com.gzyj.flink.gps.addtionalfnfo.yuebiao.JTT808_0x0200_0x66.class)
                        .deserialze(parser, type, object);
                return (T) addtionalInfo;
            }

        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x67 + ",")) {

            if (StringUtils.isNoneEmpty(this.version) && this.version.equalsIgnoreCase("2013")) {
                JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x67.class)
                        .deserialze(parser, type, object);
                return (T) addtionalInfo;
            } else {
                JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(com.gzyj.flink.gps.addtionalfnfo.yuebiao.JTT808_0x0200_0x67.class)
                        .deserialze(parser, type, object);
                return (T) addtionalInfo;
            }

        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0x70 + ",")) {

            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0x70.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;


        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0xF1 + ",")) {

            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0xF1.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;


        } else if (text.toUpperCase().contains(CHARACTERISTIC + 0xF2 + ",")) {

            JTT808_AddtionalInfo addtionalInfo = parser.getConfig().getDeserializer(JTT808_0x0200_0xF2.class)
                    .deserialze(parser, type, object);
            return (T) addtionalInfo;


        } else {

            return null;
        }
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }

    public static Integer findEndPoint(String text, int begin) {
        int stack = 0;
        int t;
        for (t = begin; t < text.length(); t++) {
            char ch = text.charAt(t);
            if (ch == '{') {
                stack++;
            }
            if (ch == '}') {
                if (stack == 0) {
                    break;
                } else {
                    stack--;
                }
            }
        }
        return t;
    }


}
